<?php

class AdminController extends BaseController {

	public function getLogin()
	{
		if (Auth::admin()->check()) return Redirect::guest('/dashboard');
		return View::make('admin.login');
	}

	public function postLogin()
	{
		if(Auth::admin()->attempt(array(
			'username' => Input::get('username'),
			'password' => Input::get('password')
		))){
		
		$admin = Auth::admin()->get();
		Session::put('admin_id',$admin->id);
		Session::put('admin_username',$admin->username);
		Session::put('admin_name',$admin->admin_name);
		Session::put('admin_admin_group_id',$admin->admin_admin_group_id);

		$log = New adminlog();
		$log->admin_log_admin_id = $admin->id;	
		$log->admin_log_activity = "Login";	
		$log->admin_log_date = date('Y-m-d H:i:s');	
		$log->save();
		return Redirect::to('/dashboard')->with('message', "Selamat datang ".$admin->username);
			
		} else {
			return Redirect::back()->with('error', "<font color='red'> Username / password salah</font><br>");
		}
	}

	public function postTambahadmin()
	{
		$rules = array(
				'password' => 'required|confirmed',
				'password_confirmation' => 'required',
			);	
			$notif = array(
				'confirmed' => ' <font color="red"> Harap masukan pasword yang sama.</font><br>',);

			$validator = Validator::make(Input::all(), $rules, $notif);

			if($validator->fails()){
				return Redirect::back()->with('error','Harap masukan pasword yang sama.');
			}
		$tambahadmin = New admin();
		$tambahadmin->admin_admin_group_id = Input::get('admin_admin_group_id');
		$tambahadmin->admin_name = Input::get('admin_name');
		$tambahadmin->username = Input::get('username');
		$tambahadmin->password = Hash::make(Input::get('password'));
		$tambahadmin->save();

		return Redirect::to('/admin/accountlist')->with('message', "Data admin berhasil ditambahkan");
	}

	public function postEditadmin($id)
	{
		
		$tambahadmin = admin::find($id);
		$tambahadmin->admin_admin_group_id = Input::get('admin_admin_group_id');
		$tambahadmin->admin_name = Input::get('admin_name');
		if(Input::get('password')<>"" OR Input::get('password_confirmation')<>"" ){
			$rules = array(
				'password' => 'required|confirmed',
				'password_confirmation' => 'required',
			);	
			$notif = array(
				'confirmed' => ' <font color="red"> Harap masukan pasword yang sama.</font><br>',);

			$validator = Validator::make(Input::all(), $rules, $notif);

			if($validator->fails()){
				return Redirect::back()->with('error','Harap masukan pasword yang sama.');
			}
			$tambahadmin->password = Hash::make(Input::get('password'));
		}
		$tambahadmin->save();

		return Redirect::to('/admin/accountlist')->with('message', "Data admin berhasil diubah");
	}


	public function postEditprofil($id)
	{
		
		$tambahadmin = admin::find($id);
		$tambahadmin->admin_name = Input::get('admin_name');
		if(Input::get('password')<>"" OR Input::get('password_confirmation')<>"" ){
			$rules = array(
				'password' => 'required|confirmed',
				'password_confirmation' => 'required',
			);	
			$notif = array(
				'confirmed' => ' <font color="red"> Harap masukan pasword yang sama.</font><br>',);

			$validator = Validator::make(Input::all(), $rules, $notif);

			if($validator->fails()){
				return Redirect::back()->with('error','Harap masukan pasword yang sama.');
			}
			$tambahadmin->password = Hash::make(Input::get('password'));
		}
		$tambahadmin->save();


		$log = New adminlog();
		$log->admin_log_admin_id = Session::get('admin_id');	
		$log->admin_log_activity = "Edit profil";	
		$log->admin_log_date = date('Y-m-d H:i:s');	
		$log->save();

		Auth::logout();
		Session::Flush();
		return Redirect::to('/admin')->with('message', "Data berhasil diubah, silahkan login kembali");
	}



	public function getProfile()
	{
		$profil = admin::find(Session::get('admin_id'));
		return View::make('admin.account.profil')->with('profil',$profil);
	}

	public function getLogout()
	{
		$log = New adminlog();
		$log->admin_log_admin_id = Session::get('admin_id');	
		$log->admin_log_activity = "Logout";	
		$log->admin_log_date = date('Y-m-d H:i:s');	
		$log->save();
		Auth::logout();
		Session::Flush();
		return Redirect::to('/admin')->with('message', "Anda telah berhasil logout");
	}

	public function getAdminhome()
	{

		return Redirect::to('/dashboard');
	}

	public function postDeleteadmin($id)
	{
		$papers = DB::table('sys_admin')
			->where('id','=',$id)
			->update(array('admin_isdelete' => 1));
		
		return Redirect::to('/admin/accountlist')->with('message','Data admin berhasil dihapus');
	}

	public function getAccountlist()
	{
		$account = DB::table('sys_admin')
					->join('sys_admin_group','admin_admin_group_id','=','admin_group_id')
					->where('id','<>',0)
					->where('admin_isdelete','=',0)
					->paginate(10);
		$role = array('0' => 'Superuser', '1'=>'Administrator');
		return View::make('admin.account.accountlist',compact('account',$account,'role',$role));
	}

	public function getDashboard()
	{
		$t = getdate(); 
   		$t = $t[0];
  		$t = date('Y',$t);

		$b = getdate(); 
	    $b = $b[0];
	    $b = date('m',$b);
	    $i = 1;

		$in = DB::table('sys_order')
		    ->where('order_status','=','Pending')
		    ->count();
		$ap = DB::table('sys_order')
		    ->where('order_status','=','Approve')
		    ->count();
		$on = DB::table('sys_order')
		    ->where('order_status','=','On Process')
		    ->count();
		$fn = DB::table('sys_order')
		    ->where('order_status','=','Finished')
		    ->count();
		$de = DB::table('sys_order')
		    ->where('order_status','=','Delivered')
		    ->count();
		$ca = DB::table('sys_order')
		    ->where('order_status','=','Cancel')
    ->count();
    
		$sodby = DB::table('sys_order')
				->whereBetween('order_date', array(date('Y-m-d', strtotime('-2 days')).' 00:00:00',date('Y-m-d', strtotime('-2 days')).' 23:59:59'))
				->count();

		$soy = DB::table('sys_order')
				->whereBetween('order_date', array(date('Y-m-d', strtotime('-1 days')).' 00:00:00',date('Y-m-d', strtotime('-1 days')).' 23:59:59'))
				->count();

		$soall = DB::table('sys_order')
				->count();

		if($soy<$sodby){
			$so = '<font color="red" size="4px">&#x25BC;         </font>'.$soy.' Order';
		}else if($soy>$sodby){
			$so = '<font color="green" size="4px">&#x25B2;         </font>'.$soy.' Order';
		}else{
			$so = '<font color="#FFD000" size="4px">=         </font>'.$soy.' Order';
		}


		$agendby = DB::table('sys_member')
				->whereBetween('member_joindate', array(date('Y-m-d', strtotime('-2 days')).' 00:00:00',date('Y-m-d', strtotime('-2 days')).' 23:59:59'))
				->count();

		$ageny = DB::table('sys_member')
				->whereBetween('member_joindate', array(date('Y-m-d', strtotime('-1 days')).' 00:00:00',date('Y-m-d', strtotime('-1 days')).' 23:59:59'))
				->count();

		$agenall = DB::table('sys_member')
				->count();

		if($ageny<$agendby){
			$agen = '<font color="red" size="4px">&#x25BC;         </font>'.$ageny.' Agen';
		}else if($ageny>$agendby){
			$agen = '<font color="green" size="4px">&#x25B2;         </font>'.$ageny.' Agen';
		}else{
			$agen = '<font color="#FFD000" size="4px">=         </font>'.$ageny.' Agen';
		}
		
		$type = DB::table('sys_order')
				->whereBetween('order_date', array(date('Y-m-d', strtotime('-1 days')).' 00:00:00',date('Y-m-d', strtotime('-1 days')).' 23:59:59'))
				->groupBy('order_name')
				->get();

		$regional = DB::table('sys_member')
				->whereBetween('member_joindate', array(date('Y-m-d', strtotime('-1 days')).' 00:00:00',date('Y-m-d', strtotime('-1 days')).' 23:59:59'))
				->groupBy('member_regional_id')
				->get();
		
		$typeall = DB::table('sys_order')
				->whereBetween('order_date', array($t.'-01-01 00:00:00', $t.'-'.$b.'-31 23:59:59'))
				->groupBy('order_name')
				->get();

		$regionalall = DB::table('sys_member')
				->whereBetween('member_joindate', array($t.'-01-01 00:00:00', $t.'-'.$b.'-31 23:59:59'))
				->groupBy('member_regional_id')
				->get();

		

	    $produk = array('brochure' => 'Brosur','idcard' => 'ID Card','packaging' => 'Packaging','tshirt' => 'T-Shirt','nota' => 'Nota');
	    $bulan = array('01' => 'Jan','02' => 'Feb','03' => 'Mar','04' => 'Apr','05' => 'May','06' => 'Jun','07' => 'Jul','08' => 'Aug','09' => 'Sep','10' => 'Oct','11' => 'Nov','12' => 'Dec');
	    $bulanex = array('01' => 'Januari','02' => 'Februari','03' => 'Maret','04' => 'April','05' => 'Mei','06' => 'Juni','07' => 'July','08' => 'Agustus','09' => 'September','10' => 'Oktober','11' => 'November','12' => 'Desember');
	    

	     $regionalexist = DB::table('sys_member')
	  			->join('ref_regional','regional_id','=','member_regional_id')
				->groupBy('member_regional_id')
				->get();

		 return View::make('admin.dashboard.dashboard',compact('in',$in,'ap',$ap,'de',$de,'on',$on,'fn',$fn,'ca',$ca,'soy',$soy,'ageny',$ageny,'regionalall',$regionalall,'typeall',$typeall,'bulanex',$bulanex,'regionalexist',$regionalexist,'t',$t, 'b',$b,'i',$i,'produk',$produk,'bulan',$bulan,'agen',$agen,'so',$so,'agenall',$agenall,'soall',$soall,'type',$type,'regional',$regional));
		





					

	}







}